package com.fwj.common.service;

import com.alibaba.fastjson.JSONObject;
import com.fwj.common.api.CodeService;
import com.fwj.common.dao.CodeMapper;
import com.fwj.common.entity.Code;
import com.fwj.common.model.CodeVo;
import com.fwj.common.util.CodeUtil;
import com.fwj.core.common.constant.DataConstant;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: fwj
 * @Data: 2022/4/25 17:51
 * @Version 1.0
 * @Description:
 */

@Service
public class CodeServiceImpl implements CodeService {

    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(CodeServiceImpl.class);

    @Autowired
    private CodeMapper codeMapper;

    @Override
    public ResponseDTO getCodeByType(JSONObject jsonObject) {
        try {
            String type = jsonObject.getString("codeType");
            //查询
            List<Code> codeList = codeMapper.selectAllCodeByType(type, DataConstant.NORMAL);
            CodeVo codeVo = buildCodeVo(codeList);
            return new ResponseDTO().success().setData(codeVo);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            e.printStackTrace();
            return new ResponseDTO(ResponseEnum.SERVER_ERROR);
        }
    }

    private CodeVo buildCodeVo(List<Code> codeList) throws Exception {
        return CodeUtil.codeListConverTree(codeList);
    }

}
